﻿using System;
using System.Windows.Forms;
using System.Reflection;

namespace RxjhServer
{
    public partial class Form9 : Form
    {
        public Form9()
        {
            InitializeComponent();

            label3.Text = label3.Text + World.服务器名;

            label1.Text += Hasher.GetIP();
            //DateTime start = Convert.ToDateTime(getWebTime());
            //DateTime end = Convert.ToDateTime(World.KeykF.SJtime);
            //int Sytime=

            //TimeSpan sp = end.Subtract(start);
            //return sp.Days;
            //int Sytime = sp.Days;
            label2.Text += World.Gamedayov.ToString()+ "天";
            tishi.Text = "时间到期后GS会自动关闭别忘记即使更新注册码。";
            if(World.Gamedayov <= 2)
            {
                textBox1.Enabled = true;
                button1.Enabled = true;
                tishi.Text = "注册码还有"+ World.Gamedayov + "天过期。GS将自动退出请及时更新!";
            }

            
            //textBox1.Copy()
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string[] kkk = textBox1.Text.Split('|');
            World.Key = kkk[0];
            World.Key2 = kkk[1];
            //World.Key2 = textBox3.Text.ToString();
            DbClss.Config.IniWriteValue("GameServer", "Key", kkk[0]);
            DbClss.Config.IniWriteValue("GameServer", "Key2", kkk[1]);
            if (yzcod())
            {

                label3.Text = label3.Text + World.服务器名;

                label1.Text += Hasher.GetIP();
                DateTime start = Convert.ToDateTime(DateTime.Now);
                DateTime end = Convert.ToDateTime(World.KeykF.SJtime);
                //int Sytime=

                TimeSpan sp = end.Subtract(start);
                //return sp.Days;
                int Sytime = sp.Days;
                label2.Text += sp.Days.ToString() + "天";

                tishi.Text = "注册码更新成功!!";
            }
            else
            {
                tishi.Text = "注册码不正确。请核对后填写!";
            }
        }

        public bool yzcod()
        {
            try
            {
                SymmetricMethod md5 = new SymmetricMethod();
                string kke = md5.Decrypto(World.Key);
                string[] kk = kke.Split('|');
                if (kk.Length >= 4)
                {
                    //DateTime Pktime = DateTime.Now;
                    //Console.WriteLine(Pktime + " " + Pktime.Millisecond);
                    if (Hasher.GetCpuID() != World.pWord(kk[0], 2) || Hasher.GetDriveID("C") != World.pWord(kk[1], 2) || Hasher.GetIP() != World.pWord(kk[2], 2) || Hasher.GetMac() != World.pWord(kk[3], 2))
                    {
                        tishi.Text = "注册码不正确！请重新输入！";
                        return false;
                    }
                    else
                    {
                        World.Keyk.CpuID = World.pWord(kk[0], 2);
                        World.Keyk.DriveID = World.pWord(kk[1], 2);
                        World.Keyk.IP = World.pWord(kk[2], 2);
                        World.Keyk.Mac = World.pWord(kk[3], 2);
                    }

                }
                else
                {
                    tishi.Text = "注册码不正确！请重新输入！";
                    return false;
                }


                Class2 md6 = new Class2();
                //SymmetricMethod md5 = new SymmetricMethod();
                string kkf = md6.DecryptByPublicKey(World.Key2);
                string[] jj = kkf.Split('|');
                if (jj.Length >= 4)
                {
                    if (Hasher.GetCpuID() != World.pWord(jj[0], 2) || Hasher.GetIP() != World.pWord(jj[2], 2) || Hasher.GetMac() != World.pWord(jj[3], 2))
                    {
                        tishi.Text = "注册码不正确！请重新输入！";
                        return false;
                    }
                    else
                    {

                        World.KeykF.CpuID = World.pWord(jj[0], 2);
                        World.KeykF.DriveID = World.pWord(jj[1], 2);
                        World.KeykF.IP = World.pWord(jj[2], 2);
                        World.KeykF.Mac = World.pWord(jj[3], 2);
                        World.KeykF.SJtime = World.pWord(jj[5], 2);
                    }
                }
                else
                {
                    tishi.Text = "注册码不正确！请重新输入！";
                    return false;
                }

                return true;
            }
            catch (Exception)
            {
                Environment.Exit(0);
                return false;
            }
        }
    }
}
